<template>
  <ych-modal
    :fullscreen="true"
    :title="title"
    :visible="visible"
    :destroyOnClose="true"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    :cancelText="$t('m.common.button.close')">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row>
          <a-col :span="7">
            <a-form-item :label="$t('m.eform.appview.entity.viewName')" :labelCol="colLayout3.labelCol" :wrapperCol="colLayout3.wrapperCol">
              <a-input v-decorator="[ 'viewName', validatorRules.viewName]" placeholder=""></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="7">
            <a-form-item :label="$t('m.eform.appview.entity.viewCode')" :labelCol="colLayout3.labelCol" :wrapperCol="colLayout3.wrapperCol">
              <a-input v-decorator="[ 'viewCode', validatorRules.viewCode]" placeholder=""></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="6">
            <a-form-item :label="$t('m.eform.apptable.entity.dsName')" :labelCol="colLayout3.labelCol" :wrapperCol="colLayout3.wrapperCol">
              <select-ds v-model="model.dsCode" placeholder=""
                         :allow-clear="false"></select-ds>
            </a-form-item>
          </a-col>
          <a-col :span="4">
            <a-form-item :label="$t('m.common.label.sort')" :labelCol="labelCol2" :wrapperCol="wrapperCol2">
              <a-input-number v-decorator="[ 'orderNum', validatorRules.orderNum]"></a-input-number>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>

      <a-radio-group v-model="type" @change="onChangeType" style="margin-bottom: 8px">
        <a-radio value="1">
          {{ $t('m.eform.formDesign.label.pu-tong-mo-shi') }} </a-radio>
        <a-radio value="2">
          {{ $t('m.eform.formDesign.label.gao-ji-mo-shi-jin-cha-xun') }} </a-radio>
      </a-radio-group>

      <TreeSingle v-if="type == '1'" ref="treeSingle" :layout="model.treeData" :groupId="groupId" :model="model"/>
      <TreeDesigner v-else-if="type == '2'" ref="treeDesigner" :layout="model.treeData" :groupId="groupId"
                    :model="model"/>

    </a-spin>
  </ych-modal>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import TreeDesigner from './actAppTree/TreeDesigner'
  import SelectDs from '../select/tag/SelectDs'
  import TreeSingle from './actAppTree/TreeSingle'
  import YchConstant from "@/api/YchConstant";
  import { GardLayoutMixin } from '@/mixins/GardLayoutMixin'

  export default {
    name: 'ActAppTreeModal',
    mixins: [GardLayoutMixin],
    components: {
      TreeDesigner,
      SelectDs,
      TreeSingle,
    },
    props: {
      groupId: {
        type: String,
        required: true
      },
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title: this.$t('m.common.label.operate'),
        visible: false,
        model: {},
        type: '1',
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        labelCol2: {
          xs: {span: 24},
          sm: {span: 12}
        },
        wrapperCol2: {
          xs: {span: 24},
          sm: {span: 12}
        },
        confirmLoading: false,
        validatorRules: {
          viewName: { rules: [{ required: true, message: this.$t('m.eform.appview.verify.viewName') },] },
          viewCode: {
            rules: [{ required: true, message: this.$t('m.eform.appview.verify.viewCode') },
              { validator: this.checkCode }]
          },
          orderNum: {
            initialValue: 1 // TODO 默认值取数据库最大值+10
          },
        },
        url: {
          add: YchConstant.PREFIX_EFORM + '/manage/actAppView/add',
          edit: YchConstant.PREFIX_EFORM + '/manage/actAppView/edit',
          checkCode: YchConstant.PREFIX_EFORM + '/manage/actAppView/checkCode',
        },
      }
    },
    methods: {
      add () {
        this.edit({})
      },
      edit (record) {
        this.form.resetFields()
        this.model = Object.assign({}, record)
        this.model.dsCode = this.model.dsCode || 'dataSourceDefault'
        if (!this.model.treeData) {
          this.type = '1'
          this.model.treeData = JSON.stringify({
            type: this.type
          })
        } else {
          const treeDataJson = JSON.parse(this.model.treeData)
          this.type = treeDataJson.type || '1'
        }
        this.visible = true
        this.$nextTick(() => {
          this.form.setFieldsValue(this.lodash.pick(this.model, 'viewName', 'viewCode', 'orderNum'))
        })
      },
      copy (record) {
        this.form.resetFields()
        this.model = Object.assign({}, record)
        this.model.id = undefined
        this.model.dsCode = this.model.dsCode || 'dataSourceDefault'
        if (!this.model.treeData) {
          this.type = '1'
          this.model.treeData = JSON.stringify({
            type: this.type
          })
        } else {
          const treeDataJson = JSON.parse(this.model.treeData)
          this.type = treeDataJson.type || '1'
        }
        this.visible = true
        this.$nextTick(() => {
          this.form.setFieldsValue(this.lodash.pick(this.model, 'viewName', 'viewCode', 'orderNum'))
        })
      },
      handleOk () {
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            let httpurl = ''
            let method = ''
            if (!this.model.id) {
              httpurl += this.url.add
              method = 'post'
            } else {
              httpurl += this.url.edit
              method = 'put'
            }
            let formData = Object.assign(this.model, values)
            formData.groupId = this.groupId
            formData.viewType = 'tree'
            if (this.type == '1') {
              this.$refs.treeSingle.getData(formData)
            } else if (this.type == '2') {
              this.$refs.treeDesigner.getData(formData)
            } else {
              this.messageUtil.warning(this.$t('m.eform.formDesign.label.lei-xing-bu-he-fa'))
              return
            }
            this.confirmLoading = true
            httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                this.messageUtil.success(res.message)
                this.$emit('ok', formData)
                this.close()
              } else {
                this.messageUtil.warning(res.message)
              }
            }).finally(() => {
              this.confirmLoading = false
            })
          }
        })
      },
      handleCancel () {
        this.close()
      },
      close () {
        this.$emit('close')
        this.visible = false
      },
      checkCode (rule, value, callback) {
        if (!value) {
          callback()
          return
        }
        let reg = new RegExp("[\\u4E00-\\u9FFF]+","g");
        if(reg.test(value)){
          callback(this.$t('m.common.tip.code_format'))
          return
        }
        getAction(this.url.checkCode, {
          value: value,
          id: this.model.id
        }).then((res) => {
          res.success ? callback() : callback(res.message)
        })
      },
      onChangeType () {
        this.model.treeData = JSON.stringify({
          type: this.type
        })
      }
    }
  }
</script>
